// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        started: {
            default: false,
            visible:false,
        },
        dH: 0,
        dS: 0,
        dL: 0,
        H: {
            get () {
                return this.dH;
            },
            set (value) {
                this.dH = value;
                if (this.started) this.Hue(value);
            },
            range: [0, 360],
            slide: true,
        },
        S: {
            get () {
                return this.dS;
            },
            set (value) {
                this.dS = value;
                if (this.started) this.Saturation(value);
            },
            range: [-1, 1],
            slide: true,
        },
        L: {
            get () {
                return this.dL;
            },
            set (value) {
                this.dL = value;
                if (this.started) this.Lightness(value);
            },
            range: [-1, 1],
            slide: true,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        this.sprite = this.node.getComponent(cc.Sprite);
        this.material = this.sprite.getMaterial(0);
        // 探索结果：不明原因原生苹台BUG可以微调H解决
        var realh = this.dH + Math.random()*0.1;
        if (realh > 360) realh = this.dH - Math.random()*0.1
        this.Hue(realh);
        this.Saturation(this.dS);
        this.Lightness(this.dL);
        this.started = true;
    },

    //色相
    Hue(value) {
        this.material.setProperty("u_dH", value);
    },

    //饱和度
    Saturation(value) {
        this.material.setProperty("u_dS", value);
    },

    //亮度
    Lightness(value) {
        this.material.setProperty("u_dL", value);
    },

    callbackH(slider) {
        this.Hue(Number(slider.progress * 360))
    },

    callbackS(slider) {
        this.Saturation(Number(slider.progress))
    },

    callbackL(slider) {
        this.Lightness(Number(slider.progress))
    },
    // update (dt) {},
});
